package com.eig.courseweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eig.common.entity.course.EduCourse;
import com.eig.common.entity.course.EduSubject;
import com.eig.common.entity.course.Teacher;
import com.eig.common.entity.system.TbMember;
import com.eig.common.vo.Result;
import com.eig.courseweb.dao.CourseWebDao;
import com.eig.courseweb.dao.TeacherDao;
import com.eig.courseweb.service.CourseWebService;
import com.eig.courseweb.utils.IdWorker;
import com.eig.courseweb.vo.CourseVo;
import com.eig.courseweb.vo.ShouCangVo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/12/30 下午 12:08
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CourseWebServiceImpl implements CourseWebService {
    @Resource
    private CourseWebDao courseWebDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    IdWorker idWorker;

    @Override
    public Result getAllMenu() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id", 1);
        wrapper.eq("parent_id", 0);
        List<EduSubject> eduCourses = courseWebDao.selectList(wrapper);
        return new Result<>(2000, "查询成功", eduCourses);
    }


    @Override
    public Result fontFindCourse(CourseVo e, PageRequest pageRequest) {
        long total = courseWebDao.count(e);
        PageImpl<EduCourse> eduSubjectsL = new PageImpl<>(this.courseWebDao.fontFindCourse(e, pageRequest), pageRequest, total);
        Map<String, Object> map = new HashMap<>();
        map.put("data", eduSubjectsL.getContent());
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result<>(2000, "查询成功", map);
    }

    @Override
    public Result findTeacherByTeacherId(String teacherId) {
        Teacher teacherByTeacherId = courseWebDao.findTeacherByTeacherId(teacherId);
        return new Result<>(2000, "查询成功", teacherByTeacherId);
    }

    @Override
    public Result findCourseByTeacherId(ShouCangVo e, PageRequest pageRequest) {
        long total = courseWebDao.counts(e);
        PageImpl<EduCourse> eduSubjectsL = new PageImpl<>(this.courseWebDao.findCourseByTeacherId(e, pageRequest), pageRequest, total);
        Map<String, Object> map = new HashMap<>();
        map.put("data", eduSubjectsL.getContent());
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result<>(2000, "查询成功", map);
    }

    @Override
    public Result getCoursByTeacherPhone(PageRequest pageRequest, String teacherMobile, String checkStatus) {
        long total = courseWebDao.countALL(teacherMobile, checkStatus);
        PageImpl<EduCourse> eduSubjectsL = new PageImpl<>(this.courseWebDao.getCoursByTeacherPhone(teacherMobile, checkStatus, pageRequest), pageRequest, total);
        Map<String, Object> map = new HashMap<>();
        map.put("data", eduSubjectsL.getContent());
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result<>(2000, "查询成功", map);
    }

    @Override
    public Result changeStatus(EduCourse e) {
        int i = courseWebDao.changeStatus(e);
        return new Result<>(2000, "修改成功", i);
    }

    @Override
    @Transactional
    public Result updateCourseSubject(EduCourse e) {
        if (ObjectUtil.isEmpty(e.getCategoryId2())) {
            e.setCategoryId2(e.getCategoryId1());
            courseWebDao.updateCourseSubject(e);
        } else {
            courseWebDao.updates(e);
        }
        courseWebDao.updateCourse(e);
        return new Result<>(2000, "操作成功");
    }

    @Override
    public Result uploadMsg() {
        return null;
    }


    @Override
    public Result InsterCourseSubject(EduCourse e) {
        Teacher id = courseWebDao.selectTeacher(e);
        System.out.println(id.getId());
        e.setId(idWorker.nextId());
        e.setTeacherId(id.getId());
        e.setIsAdd(0);
        e.setIsDeleted(0);
        e.setStatus("Draft");
        e.setLessonNum(0);
        e.setBuyCount(0);
        e.setViewCount(0);
        e.setVersion(0);
        System.out.println(e);
        if (ObjectUtil.isEmpty(e.getCategoryId2())) {
            e.setCategoryId2(e.getCategoryId1());
            courseWebDao.insterCourseSubject(e);
        } else {
            courseWebDao.inserts(e);
        }
        courseWebDao.insertCheckCourse(e);
        courseWebDao.insertCourse(e);
        return new Result<>(2000, "操作成功", e.getId());
    }


    @Override
    public Result queryAllCourseAndTeacherName(String id) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        EduCourse eduCourse = courseWebDao.selectCourse(id,null);
        EduCourse query = courseWebDao.query(id);
        queryWrapper.eq("id",eduCourse.getTeacherId());
        Teacher teacher = teacherDao.selectOne(queryWrapper);
        eduCourse.setTeacher(teacher);
        eduCourse.setDescription(query.getDescription());
        // 实体类转换为Map
        Map map = JSON.parseObject(JSON.toJSONString(eduCourse), Map.class);
        map.put("name",teacher.getName());
        map.put("avatar",teacher.getAvatar());
        map.put("mobile",teacher.getMobile());
        map.put("status",teacher.getStatus());
        map.put("intro",teacher.getIntro());
        return new Result<>(2000, "操作成功", map);
    }

    @Override
    public Result queryAllById(String id, String mobileId) {
        //先判断这个手机号是否为讲师 mobileId
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobileId);
        Teacher teacher = teacherDao.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(teacher)) {
            EduCourse eduCourse = courseWebDao.selectCourse(id, teacher.getId());
            if (ObjectUtil.isNotEmpty(eduCourse)){
                return new Result<>(2000, "操作成功", teacher);
            }
        }
        return new Result<>(5000, "该账号不是讲师", teacher);
    }

    @Override
    public Result queryMemberStatus(String id, String mobileId) {
        //先判断这个手机号是否为讲师 mobileId
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobileId);
        Teacher teacher = teacherDao.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(teacher)) {
            EduCourse eduCourse = courseWebDao.selectCourse(id, teacher.getId());
            if (ObjectUtil.isNotEmpty(eduCourse)){
                TbMember tbMember = courseWebDao.queryAll(mobileId);
                return new Result<>(2000, "操作成功", tbMember);
            }
        }
        return new Result<>(5000, "该账号不是讲师", teacher);
    }


}
